<?php
/**
 * HM肤质检测数据的model
 * Author: lyun
 * Date: 2018/3/27
 * Created by Panxsoft.
 */


namespace App\Models\Store;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * Class HmSkinRecord
 *
 * @property int id
 * @property int position
 * @property int age
 * @property string skin_name
 * @property float water
 * @property float oil
 * @property float flex
 *
 * @property-write array suggests - 评估建议数组
 * @property-write array labels - 评估结果标签数组（水、油、弹）
 * @property-write string evaluate - 年龄状态评估，因为年龄不准已废弃
 *
 * @package App\Models\Store
 * @mixin Builder
 */
class HmSkinRecord extends Model
{
    const POSITION_EYE = 1; // 眼尾
    const POSITION_CHEEK = 2; // 脸颊
    const POSITION_NECK = 3; // 颈部
    const POSITION_HAND = 4; // 手臂

    const ALL_POSITIONS = [
        self::POSITION_EYE, self::POSITION_CHEEK, 
        self::POSITION_NECK, self::POSITION_HAND 
    ];

    const POSITIONS_LOCALE_MAP = [
        'zh' => [
            self::POSITION_EYE   => '眼尾',
            self::POSITION_CHEEK => '脸颊',
            self::POSITION_NECK  => '颈部',
            self::POSITION_HAND  => '手臂',
        ],
        'en' => [
            self::POSITION_EYE   => 'eye tail',
            self::POSITION_CHEEK => 'cheek',
            self::POSITION_NECK  => 'neck',
            self::POSITION_HAND  => 'arm',
        ]
    ];

    protected $table = 'hm_skin_records';
    
    protected static $unguarded = true;
    
    protected $casts = [
        'flex'           => 'float',
        'oil'            => 'float',
        'water'          => 'float',
        'age'            => 'int',
        'position'       => 'int',
        'skin_record_id' => 'int',
        'wechat_user_id' => 'int',
    ];

    public function skinRecord(): BelongsTo
    {
        return $this->belongsTo(SkinRecord::class);
    }

    /**
     * @param array $skin_record_hm
     * @return mixed
     */
    public static function checkHasSkinRecord(array $skin_record_hm)
    {
        $hm_skin_record = self::where('skin_record_id', $skin_record_hm['skin_record_id'])
            ->where('position', $skin_record_hm['position'])
            ->first();

        return $hm_skin_record;
    }
}
